<?php

//就近 www.jiujin.me
if (!defined('IN_IA')) {
    die('Access Denied');
}
global $_W, $_GPC;
$sql = 'SELECT * FROM ' . tablename('ewei_shop_category') . ' WHERE `uniacid` = :uniacid ORDER BY `parentid`, `displayorder` DESC';
$category = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']), 'id');
$parent = $children = array();
if (!empty($category)) {
	foreach ($category as $cid => $cate) {
		if (!empty($cate['parentid'])) {
			$children[$cate['parentid']][] = $cate;
		} else {
			$parent[$cate['id']] = $cate;
		}
	}
}
load()->func('tpl');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($op == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 5;
    $list = pdo_fetchall("select * from " . tablename("ewei_shop_enoughmoney_activity") . " where uniacid=:uniacid order by id desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(":uniacid" => $_W['uniacid']));
    foreach ($list as $k => $v) {
        if (!empty($v['starttime'])) {
            $list[$k]['starttime'] = date('Y-m-d H:i:s', $v['starttime']);
        } else {
            $list[$k]['starttime'] = "";
        }
        if (!empty($v['endtime'])) {
            $list[$k]['endtime'] = date('Y-m-d H:i:s', $v['endtime']);
        } else {
            $list[$k]['endtime'] = "";
        }
        if (!empty($v['createtime'])) {
            $list[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        } else {
            $list[$k]['createtime'] = "";
        }
        if (!empty($v['em_id'])) {
            $list[$k]['em_id'] = "";
            $em_id_arr = explode(',', $v['em_id']);
            foreach ($em_id_arr as $key => $value) {
                $info = pdo_fetch("select * from" . tablename("ewei_shop_enoughmoney_set") . " where uniacid=:uniacid and id=:id ", array(":uniacid" => $_W['uniacid'], ":id" => $value));
                if ($info) {
                    $info['enoughmoney'] = intval($info['enoughmoney']);
                    $info['enoughdeduct'] = intval($info['enoughdeduct']);
                    $list[$k]['em_id'].="满" . $info['enoughmoney'] . "元减" . $info['enoughdeduct'] . "元；";
                }
            }
        }
    }
    $total = pdo_fetchcolumn("select count(*) from " . tablename("ewei_shop_enoughmoney_activity") . " where  uniacid=:uniacid order by id desc ", array(":uniacid" => $_W['uniacid']));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/shop/enoughmoney');
}
if ($op == 'post') {
    $id = intval($_GPC['id']);
    $starttime = empty($_GPC['time']['start']) ? time() : strtotime($_GPC['time']['start']);
    $endtime = empty($_GPC['time']['end']) ? strtotime('1 month') : strtotime($_GPC['time']['end']);
    if (checksubmit()) {
        empty($_GPC['title']) && message('请填写标题');
        empty($_GPC['em_id']) && message('请选择满减优惠');
        if (!empty($_GPC['goods'])) {
            $goods_id = implode(',', $_GPC['goods']);
        }
        if (!empty($_GPC['em_id'])) {
            $em_id = implode(',', $_GPC['em_id']);
        }
        $manjianinfo = pdo_fetchall("select * from " . tablename("ewei_shop_enoughmoney_set") . " where uniacid=:uniacid order by id asc", array(":uniacid" => $_W['uniacid']));
        foreach ($manjianinfo as $k => $v) {
            pdo_update("ewei_shop_enoughmoney_set", array("enoughmoney" => $_GPC['enoughmoney'][$k], "enoughdeduct" => $_GPC['enoughdeduct'][$k]), array("uniacid" => $_W['uniacid'], "id" => $v['id']));
        }
        $data = array(
            "uniacid" => $_W['uniacid'],
            'title' => $_GPC['title'],
            'em_id' => $em_id,
            'starttime' => $starttime,
            'endtime' => $endtime,
            'allgoods'=>$_GPC['allgoods'],
            'createtime' => time(),
            'status' => $_GPC['status']
        );
        if (!empty($id)) {
            pdo_update("ewei_shop_enoughmoney_activity", $data, array("uniacid" => $_W['uniacid'], "id" => $id));
        } else {
            pdo_insert("ewei_shop_enoughmoney_activity", $data);
        }
        message('活动设置成功', referer(), 'success');
    }
    if (!empty($id)) {
        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        $condition = ' uniacid=:uniacid and deleted = 0 and status=1 ';
        $params = array(':uniacid' => $_W['uniacid']);
        //获取所有商品
        $activity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_enoughmoney_activity') . " WHERE uniacid = '{$_W['uniacid']}' AND id = '{$id}'");
        $starttime = $activity['starttime'];
        $endtime = $activity['endtime'];
        if (!empty($activity)) {
            $goodsarr = explode(',', $activity['goods_id']);
            $em_id = explode(',', $activity['em_id']);
        }
        if (!empty($_GPC['goods_title'])) {
            $condition .= ' AND title=:goods_title';
            $params[':goods_title'] = trim($_GPC['goods_title']);
        }
        if (!empty($_GPC['goods_select']) && $_GPC['goods_select'] == 1) {
            if(empty($activity['goods_id'])){
            message('没有已选择的商品', referer(), 'error');
            }
            $condition .= " AND id in ({$activity['goods_id']})";
        }
        if (!empty($_GPC['category']['thirdid'])) {
		$condition .= ' AND `tcate` = :tcate';
		$params[':tcate'] = intval($_GPC['category']['thirdid']);
	}
	if (!empty($_GPC['category']['childid'])) {
		$condition .= ' AND `ccate` = :ccate';
		$params[':ccate'] = intval($_GPC['category']['childid']);
	}
	if (!empty($_GPC['category']['parentid'])) {
		$condition .= ' AND `pcate` = :pcate';
		$params[':pcate'] = intval($_GPC['category']['parentid']);
	}
        $goods = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_goods') . " WHERE $condition  ORDER BY `status` DESC, `displayorder` DESC,`id` DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, $params);
        if (!empty($goodsarr)) {
            foreach ($goods as $k => $g) {
                if (in_array($g['id'], $goodsarr)) {
                    $goods[$k]['goodsid_select'] = 1;
                }
            }
        }
        $total = pdo_fetchcolumn("select count(*) from " . tablename("ewei_shop_goods") . " where  $condition  order by id desc ", $params);
        $pager = pagination($total, $pindex, $psize);
        //获取所有满减优惠
        $manjianes = pdo_fetchall("select * from" . tablename("ewei_shop_enoughmoney_set") . " where uniacid=:uniacid order by enoughmoney asc ", array(":uniacid" => $_W['uniacid']));
        if (!empty($em_id)) {
            foreach ($manjianes as &$g) {
                if (in_array($g['id'], $em_id)) {
                    $g['em_select'] = 1;
                }
            }
        }
        foreach ($manjianes as $k => $v) {
            $manjianes[$k]['enoughmoney'] = intval($v['enoughmoney']);
            $manjianes[$k]['enoughdeduct'] = intval($v['enoughdeduct']);
        }
    } else {
        $goods = pdo_fetchall('SELECT id,title FROM ' . tablename('ewei_shop_goods') . " WHERE uniacid = '{$_W['uniacid']}' and deleted = 0 and status=1 order by id desc");
        //获取所有满减优惠
        $manjianes = pdo_fetchall("select * from" . tablename("ewei_shop_enoughmoney_set") . " where uniacid=:uniacid order by enoughmoney asc ", array(":uniacid" => $_W['uniacid']));
        foreach ($manjianes as $k => $v) {
            $manjianes[$k]['enoughmoney'] = intval($v['enoughmoney']);
            $manjianes[$k]['enoughdeduct'] = intval($v['enoughdeduct']);
        }
    }
    include $this->template('web/shop/enoughmoney');
}
//添加活动商品
if ($op == "goods_select") {
    //添加商品
    if (!empty($_GPC['submit'])) {
        $goods_select = $_GPC['goods'];
        if (empty($goods_select)) {
            message('请勾选商品', referer(), 'error');
        }
        $activity = pdo_fetch("select * from" . tablename("ewei_shop_enoughmoney_activity") . " where uniacid=:uniacid and id=1", array(":uniacid" => $_W['uniacid']));
        if (!empty($activity['goods_id'])) {
            $goods_id = explode(',', $activity['goods_id']);
            foreach ($goods_select as $k => $v) {
                if (in_array($v, $goods_id)) {
                    unset($goods_select[$k]);
                }
            }
        }
        $goods_select = implode(",", $goods_select);
        if (!empty($activity['goods_id'])) {
            $goods_select = $activity['goods_id'] . ',' . $goods_select;
        }
        pdo_update("ewei_shop_enoughmoney_activity", array("goods_id" => $goods_select), array("uniacid" => $_W['uniacid'], "id" => 1));
        message('添加商品成功', referer(), 'success');
    }
    //取消商品
    if (!empty($_GPC['clear'])) {
        $goods_select = $_GPC['goods'];
        if (empty($goods_select)) {
            message('请勾选商品', referer(), 'error');
        }
        $activity = pdo_fetch("select * from" . tablename("ewei_shop_enoughmoney_activity") . " where uniacid=:uniacid and id=1", array(":uniacid" => $_W['uniacid']));
        if (!empty($activity['goods_id'])) {
            $goods_id = explode(',', $activity['goods_id']);
            foreach ($goods_select as $k => $v) {
                if (in_array($v, $goods_id)) {
                    foreach ($goods_id as $key => $value) {
                        if ($value == $v) {
                            unset($goods_id[$key]);
                        }
                    }
                }
            }
        }
        $goods_id = implode(',', $goods_id);
        pdo_update("ewei_shop_enoughmoney_activity", array("goods_id" => $goods_id), array("uniacid" => $_W['uniacid'], "id" => 1));
        message('清除商品成功', referer(), 'success');
    }
}
//改变活动状态
if ($op == 'activity_status_set') {
    $id = intval($_GPC['id']);
    $info = pdo_fetch('select * from ' . tablename('ewei_shop_enoughmoney_activity') . " where uniacid = '{$_W['uniacid']}' and id='{$id}' ");
    if (!empty($info)) {
        $status = $info['status'] == 1 ? '0' : '1';
        pdo_update("ewei_shop_enoughmoney_activity", array('status' => $status), array("id" => $id, "uniacid" => $_W['uniacid']));
        die(json_encode(array("result" => 1, 'status' => $status)));
    } else {
        die(json_encode(array("result" => 0)));
    }
}
//删除活动
if ($op == "activity_delete") {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        message('删除失败', $this->createWebUrl('shop/enoughmoney'), 'error');
    }
    pdo_delete("ewei_shop_enoughmoney_activity", array("uniacid" => $_W['uniacid'], "id" => $id));
    message('删除成功', $this->createWebUrl('shop/enoughmoney'), 'success');
}
if ($op == 'set_list') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 5;
    $list = pdo_fetchall("select * from" . tablename("ewei_shop_enoughmoney_set") . " where uniacid=:uniacid order by enoughmoney asc  LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(":uniacid" => $_W['uniacid']));
    $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('ewei_shop_enoughmoney_set') . " where uniacid=:uniacid order by enoughmoney asc ", array(":uniacid" => $_W['uniacid']));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/shop/enoughmoney');
}
//改变状态
if ($op == 'status_set') {
    $id = intval($_GPC['id']);
    $info = pdo_fetch('select * from ' . tablename('ewei_shop_enoughmoney_set') . " where uniacid = '{$_W['uniacid']}' and id='{$id}' ");
    if (!empty($info)) {
        $status = $info['status'] == 1 ? '0' : '1';
        pdo_update("ewei_shop_enoughmoney_set", array('status' => $status), array("id" => $id, "uniacid" => $_W['uniacid']));
        die(json_encode(array("result" => 1, 'status' => $status)));
    } else {
        die(json_encode(array("result" => 0)));
    }
}
//删除
if ($op == 'delete') {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        message('删除失败', $this->createWebUrl('shop/enoughmoney', array('op' => 'set_list')), 'error');
    }
    $result = pdo_delete('ewei_shop_enoughmoney_set', array('id' => $id, 'uniacid' => $_W['uniacid']));
    if (intval($result) == 1) {
        message('删除成功', $this->createWebUrl('shop/enoughmoney', array('op' => 'set_list')), 'success');
    } else {
        message('删除失败', $this->createWebUrl('shop/enoughmoney', array('op' => 'set_list')), 'error');
    }
}
if ($op == "set_post") {
    $id = intval($_GPC['id']);
    $enough = pdo_fetch("select * from" . tablename("ewei_shop_enoughmoney_set") . " where  uniacid=:uniacid and id=:id ", array(":uniacid" => $_W['uniacid'], ":id" => $id));
    if (checksubmit()) {
        empty($_GPC['enoughmoney']) && message('请填写满金额');
        empty($_GPC['enoughdeduct']) && message('请填写优惠金额');
        $nowtime = time();
        $data = array(
            'uniacid' => $_W['uniacid'],
            "enoughmoney" => $_GPC['enoughmoney'],
            'enoughdeduct' => $_GPC['enoughdeduct'],
            'status' => $_GPC['status'],
            'createtime' => $nowtime
        );
        if (!empty($id)) {
            pdo_update("ewei_shop_enoughmoney_set", $data, array('uniacid' => $_W['uniacid'], 'id' => $id));
        } else {
            pdo_insert("ewei_shop_enoughmoney_set", $data);
        }
        message('设置成功！', $this->createWebUrl('shop/enoughmoney', array('op' => 'set_list')), 'success');
    }
    include $this->template('web/shop/enoughmoney');
}